package cn.mafangui.hotel.controller.worker;

import cn.mafangui.hotel.req.OrderQueryReq;
import cn.mafangui.hotel.entity.Order;
import cn.mafangui.hotel.enums.OrderStatus;
import cn.mafangui.hotel.response.AjaxResult;
import cn.mafangui.hotel.response.MsgType;
import cn.mafangui.hotel.response.ResponseTool;
import cn.mafangui.hotel.service.OrderService;
import cn.mafangui.hotel.utils.PropInfoConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 订单管理
 */
@Api(tags = {"操作员-订单管理"})
@RestController
@RequestMapping(value = "/op/order")
public class OpOrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 添加预订
     * 订单状态默认为未付款状态
     */
    @ApiOperation(value = "添加订单", notes = "用于预定")
    @PostMapping(value = "/add")
    public AjaxResult addOrder(int orderTypeId,String orderType, int userId,String name, String phone,int roomTypeId, String roomType,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date orderDate, Integer orderDays, Double orderCost){
        Order order = new Order(orderTypeId,orderType,userId,name,phone,roomTypeId,
                roomType,orderDate,orderDays, OrderStatus.UNPAID.getCode(),orderCost);
        int re = orderService.addOrder(order);
        if(re!=1) {
            return ResponseTool.failed(MsgType.FAILED);
        }
        return ResponseTool.success(PropInfoConstant.ADD_SUCCESS);
    }


    @ApiOperation(value ="删除订单", notes = "用于管理界面")
    @PostMapping(value = "/delete")
    public AjaxResult deleteOrder(int orderId){
        int re = orderService.delete(orderId);
        if(re!=1) {
            return ResponseTool.failed(MsgType.FAILED);
        }
        return ResponseTool.success(PropInfoConstant.DELETE_SUCCESS);
    }


    @ApiOperation(value = "更新订单")
    @PostMapping(value = "/update")
    public AjaxResult updateOrder(int orderId,int orderTypeId,String orderType, int userId,String name, String phone,int roomTypeId, String roomType,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") Date orderDate, Integer orderDays, Double orderCost,
                                  @RequestParam(value = "orderStatus", required = false) Integer orderStatus){
        Integer newOrderStatus =  ObjectUtils.isEmpty(orderStatus) ? OrderStatus.UNPAID.getCode() :
                ((orderStatus >= -3 && orderStatus <= 2) ? orderStatus :  OrderStatus.UNPAID.getCode());
        Order order = new Order(orderTypeId,orderType,userId,name,phone,roomTypeId,
                roomType,orderDate,orderDays, newOrderStatus,orderCost);
        order.setOrderId(orderId);
        int re =  orderService.update(order);
        if(re!=1) {
            return ResponseTool.failed(MsgType.FAILED);
        }
        return ResponseTool.success(PropInfoConstant.UPDATE_SUCCESS);
    }

    /**
     * 订单支付
     * @param orderId
     * @return
     */
    @ApiOperation(value = "支付订单")
    @PostMapping("/pay")
    public AjaxResult payOrder(int orderId){
        int re = orderService.payOrder(orderId);
        if(re!=1) {
            return ResponseTool.failed(MsgType.FAILED);
        }
        return ResponseTool.success(PropInfoConstant.PAY_SUCCESS);
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @ApiOperation(value = "取消订单")
    @PostMapping(value = "/cancel")
    public AjaxResult cancelOrder(int orderId){
        int re= orderService.cancelOrder(orderId);
        if(re!=1) {
            return ResponseTool.failed(MsgType.FAILED);
        }
        return ResponseTool.success(PropInfoConstant.CANCEL_SUCCESS);
    }

    /**
     * 订单超时
     * @param orderId
     * @return
     */
    @ApiOperation(value = "订单超时")
    @PostMapping(value = "/overtime")
    public int orderOver(int orderId){
        Order order = new Order(orderId,OrderStatus.OVERTIME.getCode());
        return orderService.update(order);
    }

    @ApiOperation(value="获得所有订单")
    @GetMapping(value = "")
    public AjaxResult getAllOrder(){
        return ResponseTool.success(orderService.AllOrders());
    }

    @ApiOperation(value = "查看订单数量")
    @GetMapping(value = "/count")
    public AjaxResult getOrderCount(){
        return ResponseTool.success(orderService.getOrderCount());
    }

    @ApiOperation(value = "根据userId查询用户下的所有订单")
    @GetMapping(value = "/user/{userId}")
    public AjaxResult getByUser(@PathVariable  int userId){
        return ResponseTool.success(orderService.selectByUserId(userId));
    }


    @ApiOperation(value = "根据订单号查询订单")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getById(@PathVariable Integer orderId){
        return ResponseTool.success(orderService.selectById(orderId));
    }

    @ApiOperation(value = "根据姓名/手机号查询订单",notes = "主要用于客户入住")
    @GetMapping(value = "/withNameAndPhone")
    public AjaxResult getByNameAndPhone(@RequestParam String name,@RequestParam String phone){
        return ResponseTool.success(orderService.selectByNameAndPhone(name,phone));
    }


    @ApiOperation(value = "根据[订单号、用户名、用户姓名、用户身份证号,手机号]查找订单")
    @GetMapping(value = "/query")
    public AjaxResult query(OrderQueryReq req){
        return ResponseTool.success(orderService.query(req));
    }


}
